package com.application.sistercityapplication.main;

import java.util.List;

import com.application.sistercityapplication.dto.ContactDto;
import com.application.sistercityapplication.json.JSONParserForGetList;
import com.application.sistercityapplication.listadapter.ContactListAdapter;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class ContactPointFragment extends Fragment {
	private ProgressBar pBar;
	private View v,v2;
	private LayoutInflater inflater2;
	private FrameLayout mainLayout;
	private String country_name;
	private ListView contactList;
	private int cId;
	private ImageView menuToggle;
	private SlidingMenu sm;
	public ContactPointFragment(int country_id,String count_name,SlidingMenu sm){
		this.cId = country_id;
		this.country_name = count_name;
		this.sm = sm;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		
		v = inflater.inflate(R.layout.new_activity_city_information_main, container,false);

		mainLayout = (FrameLayout) v.findViewById(R.id.frameForInflate);
		inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v2 = inflater2.inflate(R.layout.android_contactpoint_layout, null);
		ImageView back_icon = (ImageView) v2.findViewById(R.id.back_icon);
//		back_icon.setVisibility(View.INVISIBLE);
		menuToggle = (ImageView) v2.findViewById(R.id.menuToggle);
		menuToggle.setVisibility(View.INVISIBLE);
		pBar = (ProgressBar) v2.findViewById(R.id.progress);
		contactList = (ListView) v2.findViewById(R.id.contactListView);
		mainLayout.addView(v2);
		new DownloadContactTask(getActivity().getApplicationContext(),getActivity(),pBar).execute();
		menuToggle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sm.toggle();
			}
		});
		back_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Fragment newContent = new MainPageFragment(cId,country_name,sm);
				switchFragment(newContent);
			}
		});
		return v;
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof NewDashBoardPageActivity) {
			NewDashBoardPageActivity fca = (NewDashBoardPageActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	
	public class DownloadContactTask extends AsyncTask<String, Void, List<ContactDto>>{
		ProgressDialog pDialog;
		private Context context;
		public Activity _activity;
		private ProgressBar _resId;
		public DownloadContactTask(Context context,Activity act,ProgressBar resid) {
			super();
			this.context = context;
			this._activity = act;
			this._resId = resid;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			_resId.setVisibility(View.VISIBLE);
		}
		@Override
		protected List<ContactDto> doInBackground(String... params) {
			// TODO Auto-generated method stub
			JSONParserForGetList json = JSONParserForGetList.getInstance();
			List<ContactDto> contactList = json.getContacts(cId+"");
			return contactList;
		}
		@Override
		protected void onPostExecute(List<ContactDto> result) {
			super.onPostExecute(result);
			menuToggle.setVisibility(View.VISIBLE);
			_resId.setVisibility(View.GONE);
			ContactListAdapter conAdapter = new ContactListAdapter(result,getActivity().getApplicationContext(),getActivity());
			contactList.setAdapter(conAdapter);
			if (result != null) {
				
			} else {
				(Toast.makeText(context, "An error occured.", Toast.LENGTH_LONG)).show();
			}

		}
		
	}
}
